<template>
    <div>
        <a-form layout="inline"
                :model="searchData"
                style="margin: 10px 0">
            <a-form-item>
                <a-input v-model:value="searchData.name" placeholder="角色名称"></a-input>
            </a-form-item>
            <a-form-item>
                <a-input v-model:value="searchData.remark" placeholder="角色描述"></a-input>
            </a-form-item>
            <a-form-item>
                <a-select
                        ref="select"
                        v-model:value="searchData.deptId"
                        placeholder="所属部门"
                        style="width: 120px"
                        :options="deptList"
                        :field-names="{label:'name',value:'id'}"
                ></a-select>
            </a-form-item>
            <a-form-item>
                <a-button type="primary" @click="load">搜索</a-button>
            </a-form-item>
            <a-form-item>
                <a-button type="default" @click="rest">重置</a-button>
            </a-form-item>
        </a-form>
        <a-button type="primary" style="margin-bottom: 10px" @click="openModal({})">新增</a-button>
        <a-table :dataSource="roleList"
                 :columns="columns"
                 :pagination="pagination"
                 @change="handlerChange"
                 bordered>
            <template #dept="{text,record}">
                {{beLongDept(text)}}
            </template>
            <template v-slot:action="{text,record}">
                <a-space>
                    <a-button type="primary" ghost @click="openAuthorizationModal(record)">赋予权限</a-button>
                    <a-button type="primary" @click="openModal(record)">编辑</a-button>
                    <a-popconfirm
                            title="删除数据无法恢复，请确认是否删除？"
                            ok-text="是"
                            cancel-text="否"
                            @confirm="okDelete(record.id)"
                    >
                        <a-button type="danger">删除</a-button>
                    </a-popconfirm>
                </a-space>
            </template>
        </a-table>
        <a-modal v-model:visible="visible" title="新增或编辑" @ok="handleOk">
            <a-form v-model="role"
                    :label-col="{ span: 8 }">
                <a-form-item label="角色名称">
                    <a-input v-model:value="role.name"></a-input>
                </a-form-item>
                <a-form-item label="角色描述">
                    <a-textarea v-model:value="role.remark" auto-size></a-textarea>
                </a-form-item>
                <a-form-item label="所属部门">
                    <a-select
                            ref="select"
                            v-model:value="role.deptId"
                            placeholder="请选择"
                            style="width: 120px"
                            :options="deptList"
                            :field-names="{label:'name',value:'id'}"
                    ></a-select>
                </a-form-item>
            </a-form>
        </a-modal>
        <a-modal v-model:visible="authorizationVisible" title="授权" @ok="authorizationOk">
            <a-form v-model="role"
                    :label-col="{ span: 8 }">
                <a-form-item label="角色名称">
                    <a-input :disabled="true" v-model:value="role.name"></a-input>
                </a-form-item>
                <a-form-item label="角色描述">
                    <a-textarea :disabled="true" v-model:value="role.remark" auto-size></a-textarea>
                </a-form-item>
                <a-form-item label="拥有权限">
                    <a-select
                            v-model:value="role.permissions"
                            mode="multiple"
                            style="width: 100%"
                            placeholder="请选择"
                            :options="permissionList"
                            :field-names="{label:'name',value:'id'}"
                    ></a-select>
                </a-form-item>
            </a-form>
        </a-modal>
    </div>
</template>

<script>
    import {ref, onMounted} from 'vue'
    import axios from 'axios'
    import {message} from 'ant-design-vue';

    export default {
        name: "personnel-role",
        setup() {
            const columns = [
                {
                    title: 'ID',
                    dataIndex: 'id',
                    key: 'id',
                    align: 'center'
                },
                {
                    title: '角色名称',
                    dataIndex: 'name',
                    key: 'name',
                    align: 'center'
                },
                {
                    title: '角色描述',
                    dataIndex: 'remark',
                    key: 'remark',
                    align: 'center'
                },
                {
                    title: '所属部门',
                    dataIndex: 'deptId',
                    key: 'deptId',
                    slots: {customRender: 'dept'},
                    align: 'center'
                },
                {
                    title: '操作',
                    key: 'action',
                    slots: {customRender: 'action'},
                    align: 'center'
                }
            ]

            const role = ref({})
            const roleList = ref()
            const deptList = ref()
            const permissionList = ref()
            const searchData = ref({})
            const pagination = ref({
                current: 1,
                pageSize: 6,
            })

            const visible = ref(false);
            const authorizationVisible = ref(false);

            const load = () => {
                axios.get('/role/list', {
                    params: {
                        pageNum: pagination.value.current,
                        pageSize: pagination.value.pageSize,
                        name: searchData.value.name,
                        remark: searchData.value.remark,
                        deptId: searchData.value.deptId
                    }
                }).then(rs => {
                    console.log("查找结果：", rs)
                    roleList.value = rs.data.data.list
                    pagination.value.total = rs.data.data.total
                })
            }

            const rest = () => {
                searchData.value = {}
                load()
            }

            const handlerChange = (pageInfo) => {
                pagination.value = pageInfo
                load()
            }

            const openModal = (obj) => {
                console.log("模态框参数：", obj)
                visible.value = true;
                role.value = JSON.parse(JSON.stringify(obj))
            }

            const openAuthorizationModal = (obj) => {
                console.log("授权参数：", obj)
                authorizationVisible.value = true;
                role.value = JSON.parse(JSON.stringify(obj))
                axios.get('/permission/queryRoleAndPermissionByRoleId/' + role.value.id).then(rs => {
                    role.value.permissions = permissionsToArray(rs.data.data)
                })
            }

            const permissionsToArray = list => {
                let rs = []
                list.forEach(val => {
                    rs.push(val.permission)
                })
                return rs
            }

            const handleOk = () => {
                axios.post('/role/saveOrUpdate', role.value).then(rs => {
                    console.log("新增或修改结果：", rs)
                    if (rs.data.code == 200) {
                        visible.value = false
                        message.success("操作成功")
                        load()
                    } else {
                        message.error(rs.data.message)
                    }
                })
            }
            const authorizationOk = () => {
                axios.post('/role/authorization', role.value).then(rs => {
                    console.log("授权结果：", rs)
                    if (rs.data.code == 200) {
                        authorizationVisible.value = false
                        message.success("操作成功")
                        load()
                    } else {
                        message.error(rs.data.message)
                    }
                })
            }

            const okDelete = (id) => {
                console.log("要删除的ID：" + id)
                axios.delete('/role/delete/' + id).then(rs => {
                    if (rs.data.code == 200) {
                        message.success("删除成功")
                        load()
                    } else {
                        message.error(rs.data.message)
                    }
                })
            }

            const beLongDept = (id) => {
                if (typeof (id) == "undefined") {
                    return "未分配部门"
                }
                let rs = '未知角色'
                deptList.value.forEach((val, index) => {
                    if (val.id == id) {
                        rs = val.name
                    }
                })
                return rs
            }


            onMounted(() => {
                axios.get('/dept/queryAll').then(rs => {
                    deptList.value = rs.data.data
                })
                axios.get('/permission/queryAll').then(rs => {
                    permissionList.value = rs.data.data
                })
                load()
            })
            return {
                columns,
                pagination,
                handlerChange,
                searchData,
                load,
                rest,
                openModal,
                visible,
                handleOk,
                okDelete,
                role,
                roleList,
                deptList,
                permissionList,
                beLongDept,
                permissionsToArray,
                authorizationVisible,
                authorizationOk,
                openAuthorizationModal

            }
        }
    }
</script>

<style scoped>

</style>